// mixins.js
export const getTableData = (axios_get,axios_delete) => {
    const tableParams = reactive({
        tableData: [],
        btn_loading: false,
        table_loading: false,
        deleteId: "",
        pagination: {
            currentPage: 1,
            pageSize: 15,
            total: null
        }
    });
    const { tableData, but_loading, pagination, table_loading } = toRefs(tableParams);
    const init = async (searchParams) => {
        const params = {
            pageSize: pagination.value.pageSize,
            pageNum: pagination.value.currentPage,
            ...searchParams
        };
        table_loading.value = true
        await axios_get(params)
            .then(res => {
                table_loading.value = false
                tableData.value = res.data.rows || res.data.list|| res.data || res;
                pagination.value.total = res.data.total;
            })
            .catch(() => { });
    };
    const handleSizeChange = (pageSize) => {
        pagination.value.pageSize = pageSize;
        pagination.value.currentPage = 1;
        init();
    }
    const currentChange = (val) => {
        pagination.value.currentPage = val;
        init();
    }
    const handleDelete = (id) => {
        axios_delete({ id }).then(() => {
            init();
        });
    }
    const handleDeleteList = () => {
        if (deleteId) {
            this.$confirm("确定删除吗", "", {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    axios_delete({ id: this.deleteId }).then(() => {
                        init();
                    });
                })
                .catch(() => { });
        }
    }
    const emitSelection = (item) => {
        const arr = item.map(value => {
            return value.id;
        });
        deleteId = arr.join(",");
    }
    return {
        tableData,
        pagination,
        but_loading,
        init,
        handleSizeChange,
        currentChange,
        handleDelete,
    };
};
